﻿package com.xmap {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.sat.*;
	import flash.geom.Rectangle;
	
	public class Etile extends Sprite {
		public var walkable:Boolean;
		public var sides:Number = 0;
		public var points:Array;
		public var vectors:Array = new Array();
		public var lastPoint:Point;
		public var offset:Point;
		public var type:String = 'poly';
		private var bgData:BitmapData = new BitmapData(16, 16);
		public var tileW:int, tileH:int;
		private var lastOffset;
		
		public function Etile(tileNum:Number, bitData:BitmapData, width:int = 16, height:int = 16) {
			//Set height and width.
			this.tileW = width;
			this.tileH = height;
			
			//Set points.
			this.points = new Array(0, 0, width, 0, width, height, 0, height);
			
			//Draw shape.
			this.drawImage(tileNum, bitData);
			
			//Set vectornum.
			var vectorNum:Number = 0;
			
			//Draw the shape.
			for(var i:int = 0; i < this.points.length; i += 2) {
				//Create a vector instance.
				this.vectors['v' + vectorNum.toString()] = new Evector();
				
				//Check which run.
				if(i != this.points.length - 2) {
					//Assign vector points.
					this.vectors['v' + vectorNum.toString()].vectorFromPoints({x: this.points[i], y: this.points[i + 1]}, {x: this.points[i + 2], y: this.points[i + 3]});
				} else {
					//Assign vector points.
					this.vectors['v' + vectorNum.toString()].vectorFromPoints({x: this.points[i], y: this.points[i + 1]}, {x: this.points[0], y: this.points[1]});
				}
				
				//Find left normals and normalize them.
				this.vectors['v' + vectorNum.toString()].leftHandNormal();
				this.vectors['v' + vectorNum.toString()].leftNormalize();
				
				//Add a side, a point and a vector.
				sides++;
				vectorNum++;
			}
			
			//Set last point.
			lastPoint = new Point(this.x, this.y);
		}
		
		public function updatePosition(mapOffset) {
			//Grab difference from last ofset.
			var mapSet:int = mapOffset - this.lastOffset;
			
			//Update last offset.
			this.lastOffset = mapOffset;
			
			//Create offset.
			offset = new Point(this.x - lastPoint.x - mapSet, this.y - lastPoint.y);
			
			//Set vectornum.
			var vectorNum:Number = 0;
			
			//Create new points.
			for(var i:int = 0; i < this.points.length; i++) {
				//Check if odd or even.
				if(! (i % 2)) {
					//Set new point.
					this.points[i] = this.points[i] + offset.x;
				} else {
					this.points[i] = this.points[i] + offset.y;
				}
			}
			
			//Recreate vectors.
			for(i = 0; i < this.points.length; i += 2) {
				//Check which run.
				if(i != this.points.length - 2) {
					//Assign vector points.
					this.vectors['v' + vectorNum.toString()].vectorFromPoints({x: this.points[i], y: this.points[i + 1]}, {x: this.points[i + 2], y: this.points[i + 3]});
				} else {
					//Assign vector points.
					this.vectors['v' + vectorNum.toString()].vectorFromPoints({x: this.points[i], y: this.points[i + 1]}, {x: this.points[0], y: this.points[1]});
				}
				
				//Find left normals and normalize them.
				this.vectors['v' + vectorNum.toString()].leftHandNormal();
				this.vectors['v' + vectorNum.toString()].leftNormalize();
				
				//Increase vector num.
				vectorNum++;
			}
			
			//Set last point.
			lastPoint = new Point(this.x, this.y);
		}
		
		//Draw the background image.
		private function drawImage(col:Number, data:BitmapData) {
			//Copy the data into bg.
			bgData.copyPixels(data, new Rectangle(col * this.tileW, 0, this.tileW, this.tileH), new Point(0, 0));
			
			//Create a new bitmap and add it.
			var bg:Bitmap = new Bitmap(bgData);
			
			//Add bg to this.
			this.addChild(bg);
		}
	
		//Makes the object draggable, purely for testing purposes.
		public function makeDraggable() {
			this.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent) { e.target.startDrag(); });
			this.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent) { e.target.stopDrag(); });
		}
	}
}